package com.sanctuary.tools;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sanctuary.interfaces.IocEngine;

public class SpringEngine implements IocEngine {
	private static ApplicationContext context = null;
	private static final Log LOG = LogFactory.getLog(SpringEngine.class);
	private static final String[] contexts = {
		"applicationContext.xml",
		"applicationContext-services.xml",
		"applicationContext-daos.xml"
	};
	
	static {
		try {
			context = new ClassPathXmlApplicationContext(contexts);
		} catch (Exception ex) {
			LOG.fatal("Couldn't load Spring context",ex);
		}
	}
	public Object getBean(String name) {
		if (hasBean(name)) {
			return context.getBean(name);
		} else {
			return null;
		}
	}

	public boolean hasBean(String name) {
		return context.containsBean(name);
	}

}
